package cn.iocoder.yudao.module.pm.dal.mysql.thawrecords;

import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.pm.controller.admin.thawrecords.vo.ThawRecordsPageReqVO;
import cn.iocoder.yudao.module.pm.controller.app.thawrecords.vo.AppThawRecordsPageReqVO;
import cn.iocoder.yudao.module.pm.dal.dataobject.thawrecords.ThawRecordsDO;
import org.apache.ibatis.annotations.Mapper;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 过程管理-解冻专项台账 Mapper
 *
 * @author yisl
 */
@Mapper
public interface ThawRecordsMapper extends BaseMapperX<ThawRecordsDO> {

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    default PageResult<ThawRecordsDO> selectPage(ThawRecordsPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<ThawRecordsDO>()
                .eqIfPresent(ThawRecordsDO::getThawCode, reqVO.getThawCode())
                .geIfPresent(ThawRecordsDO::getStartTime, ObjectUtil.isEmpty(reqVO.getStartTime())?null:LocalDateTime.parse(reqVO.getStartTime() + " 00:00:00",formatter))
                .leIfPresent(ThawRecordsDO::getEndTime, ObjectUtil.isEmpty(reqVO.getEndTime())?null:LocalDateTime.parse(reqVO.getEndTime() + " 23:59:59",formatter))
                .eqIfPresent(ThawRecordsDO::getInitTemp, reqVO.getInitTemp())
                .orderByDesc(ThawRecordsDO::getId));
    }

    default PageResult<ThawRecordsDO> appSelectPage(AppThawRecordsPageReqVO reqVO) {
        LambdaQueryWrapperX<ThawRecordsDO> queryWrapperX = new LambdaQueryWrapperX<ThawRecordsDO>()
                .eqIfPresent(ThawRecordsDO::getCreator, reqVO.getCreator())
                .geIfPresent(ThawRecordsDO::getStartTime, reqVO.getStartDateTime())
                .leIfPresent(ThawRecordsDO::getEndTime, reqVO.getEndDateTime());
        switch (reqVO.getSort()) {
            case 1:
                queryWrapperX.orderByDesc(ThawRecordsDO::getStartTime);
                break;
            case 2:
                queryWrapperX.orderByAsc(ThawRecordsDO::getStartTime);
                break;
            case 3:
                queryWrapperX.orderByDesc(ThawRecordsDO::getEndTime);
                break;
            case 4:
                queryWrapperX.orderByAsc(ThawRecordsDO::getEndTime);
                break;
        }
        return selectPage(reqVO, queryWrapperX);
    }



}